import { ConfigModule_blxf } from "../Module/ConfigModule";
import { ButtonData_blxf } from "./ButtonData";
import GameData_blxf from "./GameData";



/** 网格数据 */
export class GridItemData_blxf {

    public constructor ( data: any ) {
        this.rowN = data[ 'rowN' ];
        this.colN = data[ 'colN' ];

        this.gridType  = data[ 'gridType' ];
        this.isUnlock  = data[ 'isUnlock' ];
        this.condition = data[ 'condition' ];

        this.obstacleType = data[ 'obstacleType' ];
        this.obstacleState = data[ 'obstacleState' ];
    }


    /** 行数 */
    rowN     : number = -1;
    /** 列数 */
    colN     : number = -1;

    /** 网格类型 1默认、9视频 */
    gridType : number = 1;
    /** 网格数据 */
    gridData : ButtonData_blxf[] = [];
    /** 是否已解锁 */
    isUnlock : boolean = false;
    /** 解锁条件 */
    condition: any = null;

    /** 障碍类型 */
    obstacleType: number = null;
    /** 障碍状态 */
    obstacleState: number = null;

    /** 纽扣类型列表 */
    gridButtonIdList: number[] = [];
    /** 纽扣类型对应数量列表 */
    gridButtonIdNumList: number[] = [];

    /** 获取格子坐标 */
    getGridPos_blxf () {
        return {
            rowN : this.rowN,
            colN : this.colN
        }
    }

    /** 检测是否解锁 */
    onCheckIsUnlock_blxf () {
        return this.isUnlock;
    }

    /** 获取格子类型 */
    getGridType_blxf () {
        return this.gridType;
    }

    /** 解锁网格 */
    setUnlock_blxf () {
        this.isUnlock = true;
    }

    /** 获取障碍信息 */
    getObstacleInfo_blxf () {
        return {
            obstacleType : this.obstacleType,
            obstacleState: this.obstacleState
        }
    }

    /** 获取障碍类型 */
    getObstacleType_blxf () {
        this.obstacleType;
    }

    /** 设置障碍初始数据 */
    setObstacleType_blxf ( obstacleType: number ) {
        this.obstacleType = obstacleType;
        this.obstacleState = 0;
        // cc.log(this.rowN, this.colN, 'this.obstacleType >11>', this.obstacleType, this.obstacleState);
    }

    /** 更新障碍物状态 */
    updateObstacleState_blxf ( addNum: number = 1 ) {
        this.obstacleState += addNum;
    }

    /** 检测是否是障碍 */
    onCheckObstacle_blxf () {
        return this.obstacleType && this.obstacleState < 3;
    }

    /**
     * 添加纽扣数据
     * @param data 纽扣数据
     */
    addButtonData_blxf ( data: any ) {
        /** 设置障碍类型 */
        let { obstacleType } = data;
        obstacleType && this.setObstacleType_blxf( obstacleType );

        let _data: ButtonData_blxf = new ButtonData_blxf( data );
        this.gridData.push( _data );

        this.onCheckButtonIdList_blxf();
    }

    /** 检测纽扣id列表 */
    onCheckButtonIdList_blxf () {
        this.gridButtonIdList.length = 0;
        this.gridButtonIdNumList.length = 0;

        this.gridData.forEach( item =>{
            let _buttonId: number = item.getButtonId_blxf();
            let _buttonIdNum: number = item.getButtonIdNum_blxf();
            let _endIndex: number = this.gridButtonIdList.length - 1;
            let _isCheck = this.gridButtonIdList[ _endIndex ] == _buttonId;
            !_isCheck && this.gridButtonIdList.push( _buttonId );

            let _index: number = _isCheck ? _endIndex : _endIndex + 1;
            this.gridButtonIdNumList[ _index ] = this.gridButtonIdNumList[ _index ] || 0;
            this.gridButtonIdNumList[ _index ] += _buttonIdNum;
        })
        // cc.log('gridButtonIdList >>', this.gridButtonIdList);
    }

    /**
     * 移除纽扣数据
     * @param buttonId 纽扣id
     */
    removeButtonData_blxf ( buttonId: number ) {
        let _buttonId: number = this.getTopButtonId_blxf();
        if ( buttonId != _buttonId ) return;

        let _buttonN: number = this.gridData.length;
        for ( let i = _buttonN - 1; i >= 0; i-- ) {
            let _buttonData: ButtonData_blxf = this.gridData[ i ];
            let _checkId: number = _buttonData.getButtonId_blxf();
            if ( _checkId != buttonId ) break;

            this.gridData.splice( i );
            // cc.log('remove ===>>', this.rowN, this.colN, buttonId);
        }

        this.onCheckButtonIdList_blxf();
    }

    /** 清除网格纽扣数据 */
    clearAllButtonData_blxf () {
        this.gridData.length = 0;

        this.onCheckButtonIdList_blxf();
    }

    /**
     * 获取最上层纽扣id
     * @returns 返回纽扣id
     */
    getTopButtonId_blxf ( filterButtonIds: number[] = [] ) {
        let _buttonId: number;

        let _len: number = this.gridButtonIdList.length;
        if ( _len == 0 ) return _buttonId;
        
        let _checkIndex: number = 0;
        let _checkButtonIds: number[] = [ ...filterButtonIds ];

        for ( let i = _len - 1; i >= 0; i-- ) {
            _buttonId = this.gridButtonIdList[ i ];
            let _index = _checkButtonIds.indexOf( _buttonId );
            if ( _index != _checkIndex ) break;

            _checkIndex += 1;
        }

        return _buttonId;
    }

    /**
     * 获取最上层纽扣id个数
     * @returns 返回相同数
     */
    getTopButtonIdNum_blxf () {
        let _alikeN: number = 0;

        let _len: number = this.gridData.length;
        let _checkButtonId: number = this.getTopButtonId_blxf();

        for ( let i = _len - 1; i>= 0; i-- ) {
            let _buttonItem: ButtonData_blxf = this.gridData[ i ];
            let _buttonId: number = _buttonItem.getButtonId_blxf();
            if ( _buttonId != _checkButtonId ) break;

            let _buttonN: number = _buttonItem.getButtonIdNum_blxf();
            _alikeN += _buttonN;
        }

        return _alikeN;
    }

    getGridButtonAllIds_blxf () {
        let _allIds: number[] = [];

        this.gridData.forEach( item =>{
            let _buttonId: number = item.getButtonId_blxf();
            _buttonId && _allIds.push( _buttonId );
        })

        return _allIds;
    }

    /** 获取纽扣id列表 */
    getGridButtonIdList_blxf () {
        return this.gridButtonIdList;
    }

    getGridButtonIdNumList_blxf () {
        return this.gridButtonIdNumList;
    }

    /**
     * 检测网格
     * @param rowN 行 
     * @param colN 列
     * @returns 返回是否是当前网格
     */
    onCheckGridPos_blxf ( rowN: number, colN: number ) {
        let _isRow: boolean = this.rowN == rowN;
        let _isCol: boolean = this.colN == colN;
        return _isRow && _isCol;
    }

    /**
     * 检测是否是空网格
     * @returns 返回结果
     */
    onCheckNull_blxf () {
        let _isNull: boolean = this.gridData.length == 0;
        
        if ( !this.isUnlock ) _isNull = false;
        
        return _isNull;
    }

    /**
     * 检测是否可消除
     * @returns 返回消除结果
     */
    onCheckRemoveOk_blxf () {
        let _buttonId: number;

        let { checkButtonNum } = ConfigModule_blxf;
        let _alikeN: number = this.getTopButtonIdNum_blxf();
        let _isOk: boolean = _alikeN >= checkButtonNum;
        if ( !_isOk ) return _buttonId;

        _buttonId = this.getTopButtonId_blxf();
        for ( let i = 0; i < _alikeN; i++ ) {
            this.removeButtonData_blxf( _buttonId );
        }

        return _buttonId;
    }
    
    /** 更新网格数据存储 */
    updateGridData_blxf () {
        let _buttonIds: number[] = [];
        this.gridData.forEach( item =>{
            let _buttonId: number = item.getButtonId_blxf();
            _buttonId && _buttonIds.push( _buttonId );
        })
        // cc.warn('_buttonIds >>', this.rowN, this.colN, _buttonIds);
        GameData_blxf.instance.setGridData_blxf({
            rowN: this.rowN, colN: this.colN, dataList: _buttonIds
        })
    }
    
}
